package com.example.testservice;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.core.app.NotificationCompat;

import java.util.Timer;
import java.util.TimerTask;

public class MyService extends Service {

    Binder _binder = new Binder();
    Thread _thread = null;
    boolean _started = false;

    Timer _timer = new Timer();

    public MyService() {
    }

    @Override
    public void onCreate() {
        Log.d("service", "create");
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        Log.d("service", "destroy");

        _started = false;
        try {
            _thread.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        _timer.cancel();

        super.onDestroy();
    }

    void makeNotify() {
        Log.d("service", "notify");

        Context context = getApplicationContext();
        String id = "my_channel_01";
        String name = "我是渠道名字";
        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel mChannel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_LOW);
            Toast.makeText(this, mChannel.toString(), Toast.LENGTH_SHORT).show();
            Log.i("service", mChannel.toString());
            notificationManager.createNotificationChannel(mChannel);
            notification = new Notification.Builder(context, id)
                    .setChannelId(id)
                    .setContentTitle("5 new messages")
                    .setContentText("hahaha")
                    .setSmallIcon(R.mipmap.ic_launcher).build();
        } else {
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, id)
                    .setContentTitle("5 new messages")
                    .setContentText("hahaha")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setOngoing(true);
            notification = notificationBuilder.build();
        }
        notificationManager.notify(111123, notification);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("service", "start");

        _timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.i("service", "timer");

                new Handler(Looper.getMainLooper()) {
                    @Override
                    public void handleMessage(Message msg) {
                        makeNotify();
                    }
                }.obtainMessage().sendToTarget();
            }
        }, 300, 7000);

        _started = true;
        _thread = new Thread(() -> {
            Log.d("service", "thread start");

            while (_started) {
                Log.d("service", "toast");

                // 后台弹框需要权限
                new Handler(Looper.getMainLooper()) {
                    @Override
                    public void handleMessage(Message msg) {
                        Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
                    }
                }.obtainMessage().sendToTarget();

                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }

            Log.d("service", "thread end");
        });
        _thread.start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("service", "bind");
        return _binder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("service", "unbind");
        return super.onUnbind(intent);
    }
}